package jTopaz;

import java.util.Timer;
import java.io.File;
import java.util.TimerTask;
import org.globus.ftp.GridFTPClient;
import org.ietf.jgss.GSSCredential;

/**
Handles monitoring the size/updating the progress of an upload.
*/
public class RemoteFileMonitor {

    private String fileName;
    private long size;
    private Timer timer;
    private RemoteMonitorTask task;
	private GridFTPClient client;
	
	/**
	@param fileName the remote file to monitor
	@client the client to use for polling.
	@cred a GSSCredential, (get rid of this)
	*/
    public RemoteFileMonitor(String fileName, long size, GridFTPClient client, GSSCredential cred) {
    	try {
    	
    	this.client = client;
		this.fileName = fileName;
		this.size = size;
		this.task = new RemoteMonitorTask(fileName, size, client, cred);
		timer = new Timer(true);
		System.out.println(client.getSize(fileName));
    	}
    	catch (Exception e) {
    		System.out.println(e);
    	}
    }

	/**
	What is actually executed.  Creates a RemoteTask object, and schedules it for every second.
	*/
    public void run() {
		long delay = 0;
		long period = 1000;
		timer.schedule(task, delay, period);
    }

	/**
	cancels the task
	*/
    public void stop() {
		task.cancel();
    }
    
    /**
    Gets the current speed of the transfer
    */
    public double getSpeed() {
    	return task.getSpeed();	
    }
    
    /**
    Gets the current progress of the transfer.
    */
    public double getProgress() {
    	return task.getProgress();
    }
}

/**
Does this show up?
*/
class RemoteMonitorTask extends TimerTask{

    private String fileName;
    private double size;
    private double previous;
    private GridFTPClient client;
	private double speed;
	private double progress;
	
    public RemoteMonitorTask(String fileName, long size, GridFTPClient client, GSSCredential cred) {
    try {
	this.fileName = fileName;
	this.size = new Long(size).doubleValue();
	previous = 0;
	this.client = client;
	//client = new GridFTPClient("rocks-97.sdsc.edu", 2811);
	//System.out.println("new cred created");
	//client.authenticate(cred);
	
	speed = 0;
	progress = 0;
    }
    catch(Exception e) {
    	System.out.println(e);
    	e.printStackTrace();
    }
    }

    public void run() {

	try {
	  
	  	//System.out.println("in the monitor???");
	  	//System.out.println("do nothing?");
	  	//System.out.println(client);
	  	//if (true)
	  	//	return;
	  	
	   	client.setPassive();
	   	client.setLocalActive();
	   	
	   	if (!client.exists(fileName)) {
	   		System.out.println("file hasn't been created yet");
	   		return;
	   	}
	   	
	   	else {
	   		System.out.println("do something");
	   

	   	
	   	 	double currentSize = new Long(client.size(fileName)).doubleValue();
	    	System.out.println(currentSize);
	    
	    
	    	double change = currentSize - previous;
	    	speed = change/1024;
		   	System.out.println("Speed: " + speed + " kb/s");
	   		System.out.println("total size: " + size);
	    	progress = currentSize/size * 100;
	    	System.out.println("Progress: " + progress + "%\n");
	    
	    	previous = currentSize;
	   	}
	    
	}
	catch (Exception e) {
	    System.out.println(e);
	    e.printStackTrace();
	}
    }
    
    public double getSpeed() {
    	return speed;
    }
    
    public double getProgress() {
    	return progress;
    }
}